let router = require('../router/Router');

let Token = require('../token');
let ipUtil = require('../util/ipUtil');

let userService = require('../service/userService');
let smsService = require('../service/SmsService');

const {BusiError, SysError} = require('../exception/exception');

/**
 * 添加代理关系接口
 */
router.post('/auth/user/addAgentRelationship', (req, resp) => {
    let agent = req.body.agent;
    if(!agent){
        resp.send({code: 500, msg: "无效代理信息"});
        return;
    }

    userService.addAgentRelationship(req.uid, req.body.agent).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        let msg = "系统异常, 请稍后再试";
        console.logw("err:", err);
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 用户登录接口
 */
router.post("/user/login", function (req, resp) {
    let phone = req.body.acct;
    let password = req.body.pwd;
    // if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
    //     console.logg("手机号码格式不对 ", phone);
    //     resp.send({code: 500, msg: '手机号码格式不对'});
    //     return;
    // }

    if (password && password.length < 8) {
        resp.send({code: 500, msg: '请输入8位以上的密码'});
        return;
    }

    // let pwd = Token.aesEncodeDef(password);
    let ip = ipUtil.getIp(req, resp);
    userService.loginByPwd(phone, password, ip).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        let msg = "系统异常, 请稍后再试";
        console.logw("err:", err);
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 用户头像修改接口
 */
router.post("/auth/info/avatar/modify", (req, resp) => {
    if (req.body.id == null || req.body.id === undefined || req.body.id === ''){
        resp.send({code: 500, msg: "头像不能为空"});
    }

    userService.modifyAvatar(req.uid, req.body.id).then(()=>{
        resp.send({code: 200, msg: "修改成功"});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err});
    });
});

/**
 * 密码修改接口
 */
router.post('/auth/user/pwd/modify', (req, resp) => {
    if (!req.body.prePwd || req.body.prePwd === ''){
        resp.send({code: 500, msg: "请输入旧密码"});
        return;
    }

    if (!req.body.newPwd || req.body.newPwd === ''){
        resp.send({code: 500, msg: "请输入新密码"});
        return;
    }

    if (req.body.newPwd.length < 8){
        resp.send({code: 500, msg: "新密码长度必须大于等于8"});
        return;
    }

    userService.modifyPwd(req.uid, req.body.prePwd, req.body.newPwd).then(() => {
        resp.send({code: 200, msg: "更新密码成功"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 获取密码修改手机验证码
 */
router.post('/user/pwd/modify/code/get', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    if (!req.body.mobile || req.body.mobile === ''){
        resp.send({code: 500, msg: "手机号不能为空"});
        return;
    }

    smsService.getModifyPwdValidCode(ip, req.body.mobile, req.body.countryCode).then(()=>{
        resp.send({code: 200, msg: "发送验证码成功, 请留意您的手机"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 通过验证码修改用户密码接口
 */
router.post('/user/pwd/modify/code', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    if (!req.body.mobile || req.body.mobile === ''){
        resp.send({code: 500, msg: "手机号不能为空"});
        return;
    }

    // if (!(/^1(3|4|5|7|8)\d{9}$/.test(req.body.mobile))) {
    //     console.logg("手机号码格式不对 ", req.body.mobile);
    //     resp.send({code: 500, msg: '请输入正确的手机号码'});
    //     return;
    // }

    if (!req.body.pwd || req.body.pwd.length < 8) {
        resp.send({code: 500, msg: '密码长度必须大于等于8'});
        return;
    }

    if (!req.body.code || req.body.code.length !== 4){
        resp.send({code: 500, msg: '验证码有误'});
        return;
    }

    userService.modifyPwdByCode(req.body.mobile, req.body.pwd, req.body.code).then(()=>{
        resp.send({code: 200, msg: "密码重置成功"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 获取注册验证码接口
 */
router.post('/user/register/mobile/code', (req, resp) => {
    let phone = req.body.mobile;
    // if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
    //     console.logg("手机号码格式不对 ", phone);
    //     resp.send({code: 500, msg: '请输入正确的手机号码'});
    //     return;
    // }

    let countryCode = req.body.countryCode;
    if(!countryCode || countryCode === ''){
        console.logw("国家编码不能为空 ", phone);
        resp.send({code: 500, msg: '国家编码不能为空'});
        return;
    }

    let ip = ipUtil.getIp(req, resp);
    smsService.getRegisterCode(ip, phone, countryCode).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 通过token或者uuid登录接口
 */
router.post('/user/login/token', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    let token = req.body.token;
    let uid = req.body.uid;
    let code = req.body.code;
    if(token || uid){
        userService.loginByTokenOrUUID(req.body.token, req.body.uid,  req.ip, req.ua, "").then(res => {
            resp.send({code: 200, msg: "success", data: res});
        }).catch(e => {
            console.logw("err:", e);
            let msg = "系统异常, 请稍后再试";
            if (e instanceof BusiError) {
                msg = e.message;
            }
            resp.send({code: 500, msg: msg});
        });
        return;
    }
    userService.loginByWxCode(ip, code);
});

router.post('/auth/user/agent/link', (req, resp) => {
    userService.getPromoteLink(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 用户二维码修改接口
 */
router.post("/auth/info/qrcode/modify", (req, resp) => {
    if (req.body.qrcode == null || req.body.qrcode === undefined || req.body.qrcode === ''){
        resp.send({code: 500, msg: "二维码不能为空"});
    }

    userService.modifyQrcode(req.uid, req.body.qrcode).then(()=>{
        resp.send({code: 200, msg: "修改成功"});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err});
    });
});

/**
 * 通过token或者uuid登录接口
 */
router.post('/user/info', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    userService.getUserInfo(req.body.uid,  req.ip, req.ua, "").then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 赠送房卡
 */

router.post('/auth/sendFangka', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    userService.sendFangka(req.uid,  req.body.sendUid, req.body.cardNum).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 获取用户详细信息
 */
router.post('/auth/user/info', (req, resp) => {
    let ip = ipUtil.getIp(req, resp);
    userService.getUserInfo(req.uid, ip).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

//快3 
//战绩接口
router.post('/auth/user/getMyK3Record', (req, resp) => {
    console.logw("快3 战绩接口 :", req.body.typePlace,req.body.typeAward,req.body.page);
    let typePlace = 0;
    if (req.body.typePlace){
        if(req.body.typePlace >= 0 && req.body.typePlace <= 2){
            typePlace = req.body.typePlace;
        }
    }

    let typeAward = 0;
    if (req.body.typeAward){
        if(req.body.typeAward >= 0 && req.body.typeAward <= 2){
            typeAward = req.body.typeAward;
        }
    }
    let page = 1;
    if(req.body.page){
        if(req.body.page >= 1){
            page = req.body.page;
        }
    }

    userService.getMyK3Record(req.uid, typePlace, typeAward,page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

//推广接口
//推广初始化
router.post('/auth/user/popularizeK3Init', (req, resp) => {

    userService.popularizeK3Init(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

//领取奖励接口
router.post('/auth/user/getk3AdminAward', (req, resp) => {

    userService.getk3AdminAward(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

//推广明细
router.post('/auth/user/getk3AdminInfo', (req, resp) => {
    let mType = req.body.mType;
    if(!mType){
        mType = 0;
    }
    if(mType < 0 || mType > 2){
        mType = 0;
    }

    let page = req.body.page;
    if(!page){
        page = 1;
    }
    if(page < 1){
        page = 1;
    }
    //0 1 2 今月 上月 全部
    userService.getk3AdminInfo(req.uid,mType,page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

//我的
//资金明细
router.post('/auth/user/getk3ScoreInfo', (req, resp) => {

    let mType = req.body.mType;
    if(!mType){
        mType = 0;
    }
    if(mType < 0 || mType > 2){
        mType = 0;
    }

    let page = req.body.page;
    if(!page){
        page = 1;
    }
    if(page < 1){
        page = 1;
    }
    //0 1 2 今月 上月 全部
    userService.getk3ScoreInfo(req.uid,mType,page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});